// featurePackage/pages/works/worksPreview/worksPreview.js
import {
  queryProductDetail,
} from "../../../../service/jb_product_service"
import {
  checkWorksReq,
  worksDetailReq
} from "../../../../service/jb_activity_service"
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    swiperAutoplay: true,
    swiperInterval: 3500,
    swiperDuration: 500,
    currentSwiperIndex: 0,//当前swiper下标
    detailInfo: {},//详情
    actionType: 0,
    userInfo: {},
    productDetail: {},
    showRefundPop: false,
    worksId: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  //action 0:自己预览  1:作品审核者预览 2:模版预览
  onLoad: function (options) {
    if (JSON.stringify(app.globalData.worksPreview) != "{}") {
      this.setData({
        detailInfo: app.globalData.worksPreview,
      })
      // app.globalData.worksPreview = {};
    }
    console.log("JJLog:当前详情数据：" + JSON.stringify(this.data.detailInfo));
    this.setData({
      actionType: options.action,
      userInfo: app.globalData.userInfo,
    })
    console.log("JJLog:个人信息是：" + JSON.stringify(this.data.userInfo));
    if (this.data.detailInfo && this.data.detailInfo.productId) {
      this.queryProductDetail();
    }
    if (options.worksId) {
      this.setData({
        worksId: options.worksId,
      })
      this.queryWorksDetail();
    }

  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  queryWorksDetail() {
    wx.showLoading()
    worksDetailReq(this.data.worksId).then(res => {
      wx.hideLoading()
      if (res.code == 200) {
        this.setData({
          detailInfo: res.data,

        })
        if (res.data.products && res.data.products.length > 0) {
          this.setData({
            productDetail: res.data.products[0],
          })
        }
      }
    }).catch(err => {
      wx.hideLoading()
      wx.showToast({
        title: err || '查询失败',
        icon: "none"
      })
      console.error("JJLog:worksDetailReq", err);
    })
  },


  queryProductDetail() {
    queryProductDetail(this.data.detailInfo.productId, 'normal').then(res => {
      console.log("JJLog:新商品详情是=====:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          productDetail: res.data.productInfo,
        })
      }
    }).catch(err => {
      console.error("JJLog:queryProductDetail", err);
    })
  },
  //顶部swiper切换方法:
  topImgSwiperChange(e) {
    this.setData({
      currentSwiperIndex: e.detail.current
    })
  },
  //预览图片:
  previewImgAction(e) {
    let url = e.currentTarget.dataset.url;
    let arr = e.currentTarget.dataset.arr;
    wx.previewImage({
      current: url,
      urls: arr,
    })
  },
  pushToProduct() {
    wx.navigateTo({
      url: '/shopMallPackage/pages/newStoreDetail/newStoreDetail?houseNumber=' + this.data.productDetail.id + "&type=normal",
    })
  },

  // 拒绝作品通过
  refuseWorksAction() {
    this.setData({
      showRefundPop: true,
    })
  },
  //同意作品通过
  agreeWorksAction() {
    wx.showModal({
      title: '提示',
      content: '确认作品通过审核?',
      confirmColor: "#ff0000",
      success: res => {
        if (res.confirm) {
          let req = {
            voitId: this.data.detailInfo.voteId,
            id: this.data.worksId,
            examineStatus: 1,
          }
          wx.showLoading();
          checkWorksReq(req).then(res => {
            wx.hideLoading();
            if (res.code == 200) {
              wx.showToast({
                title: '操作成功',
                icon: "success"
              })
              setTimeout(() => {
                wx.navigateBack();
              }, 2000)
            }
          }).catch(err => {
            wx.hideLoading();
            wx.showToast({
              title: err || "操作失败",
              icon: "none"
            })
            console.error("JJLog:checkWorksReq", err);
          })
        }

      }
    })

  },
  //确认拒绝原因
  refundSureAction(e) {
    let valueText = e.detail.comment;
    let req = {
      voitId: this.data.detailInfo.voteId,
      id: this.data.worksId,
      examineStatus: 2,
      examineRemark: valueText,
    }
    wx.showLoading();
    checkWorksReq(req).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        wx.showToast({
          title: '操作成功',
          icon: "success"
        })
        this.setData({
          showRefundPop: false
        })
        setTimeout(() => {
          wx.navigateBack();
        }, 2000)
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "操作失败",
        icon: "none"
      })
      console.error("JJLog:checkWorksReq", err);
    })
  },
  refundPopHide() {
    this.setData({
      showRefundPop: false,
    })
  },
})